import time

from hypium import BY
from hypium.model import MatchPattern

from devicetest.core.test_case import TestCase, Step, CheckPoint
from hypium import *
# 类名 要跟文件名保持一致！
class JiaoYinShiLuoDe_010(TestCase):
    def __init__(self, controllers):
        self.TAG = self.__class__.__name__
        TestCase.__init__(self, self.TAG, controllers)
        self.driver = UiDriver(self.device1)

    def setup(self):
        Step('1.回到桌面')
        self.driver.swipe_to_home()
    # 打开“交银基金”App
    def process(self):
        Step('打开“交银基金”APP')
        self.driver.start_app(package_name="com.fund001.etradingapp.hos")

        # 这里需要等待5s才能进入到首页，因为有广告
        time.sleep(5)
        # ========== 新增 CheckPoint 验证 非首次进入App首页 ==========
        CheckPoint('进入App首页')
        home_page_btn = self.driver.find_component(BY.text("首页"))
        assert home_page_btn is not None, "未找到“首页”按钮"

        Step('测试1：充值取现，预期：（在未登录时）跳转到登录界面，定位到“手机登录”来验证')
        self.driver.find_component(BY.text("充值取现"))
        self.driver.touch(BY.text("充值取现"))

        time.sleep(1)
        # ========== CheckPoint 验证：（在未登录时）跳转到登录界面， ==========
        bt1=self.driver.find_component(BY.text("手机登录"))
        assert bt1 is not None, "未找到“手机登录”按钮"

        # 验证完之后返回
        # 定位左上角返回按钮（xpath）
        back_btn = self.driver.find_component(
            BY.xpath("/root/Stack/Row[1]/Web/rootWebArea/genericContainer[1]/genericContainer/genericContainer/genericContainer/genericContainer/genericContainer/genericContainer/image[1]")

        )
        assert back_btn is not None, "未找到返回按钮控件"  # 先验证控件是否存在
        # 点击返回按钮
        self.driver.touch(BY.xpath("/root/Stack/Row[1]/Web/rootWebArea/genericContainer[1]/genericContainer/genericContainer/genericContainer/genericContainer/genericContainer/genericContainer/image[1]")
            )

        time.sleep(1)

        Step('测试2：盘后估值，预期：进入界面+点击到别处逛逛')
        self.driver.find_component(BY.text("盘后估值"))
        self.driver.touch(BY.text("盘后估值"))

        # ========== CheckPoint 验证：跳转到界面==========
        bt2 = self.driver.find_component(BY.text("别处逛逛"))
        assert bt2 is not None, "未找到“别处逛逛”按钮"
        self.driver.touch(BY.text("别处逛逛"))
        time.sleep(1)

        Step('测试3：投顾净值，预期：进入界面+返回按钮')
        self.driver.find_component(BY.text("投顾净值"))
        self.driver.touch(BY.text("投顾净值"))
        # ========== CheckPoint 验证：跳转到界面==========
        bt3= self.driver.find_component(BY.text("投顾"))
        assert bt3 is not None, "未找到“投顾”按钮"
        # 点击：返回按钮
        # 定位左上角返回按钮（xpath）
        back_btn = self.driver.find_component(
            BY.xpath("/root/Stack/Row[1]/Web/rootWebArea/genericContainer[1]/genericContainer[2]/genericContainer/genericContainer/genericContainer/genericContainer/genericContainer/genericContainer/genericContainer[1]/genericContainer[1]/image")

        )
        assert back_btn is not None, "未找到返回按钮控件"  # 先验证控件是否存在
        # 点击返回按钮
        self.driver.touch(BY.xpath("/root/Stack/Row[1]/Web/rootWebArea/genericContainer[1]/genericContainer[2]/genericContainer/genericContainer/genericContainer/genericContainer/genericContainer/genericContainer/genericContainer[1]/genericContainer[1]/image")
            )

        time.sleep(1)

        Step('测试4：投顾课程，预期：进入界面+返回按钮')
        self.driver.find_component(BY.text("投顾课程"))
        self.driver.touch(BY.text("投顾课程"))
        # ========== CheckPoint 验证：跳转到界面==========
        bt4 = self.driver.find_component(BY.text("宽基指数"))
        assert bt4 is not None, "未找到“宽基指数”按钮"
        # 点击：返回按钮
        # 定位左上角返回按钮（xpath）
        back_btn = self.driver.find_component(
            BY.xpath("/root/Column/Row[1]/Image")
        )
        assert back_btn is not None, "未找到返回按钮控件"  # 先验证控件是否存在
        # 点击返回按钮
        self.driver.touch(BY.xpath("/root/Column/Row[1]/Image"))

        time.sleep(1)

        Step('测试5：积分中心，预期：（在未登录时）跳转到登录界面，定位到“手机登录”来验证')
        self.driver.find_component(BY.text("积分中心"))
        self.driver.touch(BY.text("积分中心"))
        time.sleep(1)

        # ========== CheckPoint 验证：（在未登录时）跳转到登录界面， ==========
        bt5 = self.driver.find_component(BY.text("手机登录"))
        assert bt5 is not None, "未找到“手机登录”按钮"

        # 验证完之后返回
        # 定位左上角返回按钮（xpath）
        back_btn = self.driver.find_component(
            BY.xpath("/root/Stack/Row[1]/Web/rootWebArea/genericContainer[1]/genericContainer/genericContainer/genericContainer/genericContainer/genericContainer/genericContainer/image[1]")

        )
        assert back_btn is not None, "未找到返回按钮控件"  # 先验证控件是否存在
        # 点击返回按钮
        self.driver.touch(BY.xpath("/root/Stack/Row[1]/Web/rootWebArea/genericContainer[1]/genericContainer/genericContainer/genericContainer/genericContainer/genericContainer/genericContainer/image[1]")
)

        time.sleep(1)

    def teardown(self):
        Step('关闭交银基金APP')
        self.driver.stop_app("com.fund001.etradingapp.hos")